﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using Kinlo.MesCheck;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Kinlo.UI.Main.View
{
    /// <summary>
    /// MesSettingView.xaml 的交互逻辑
    /// </summary>
    public partial class MesSettingView : Window
    {
        public MesSettingView()
        {
            InitializeComponent();
            Init();
        }
        void Init()
        {
            try
            {
                //BatchNoCheck.IsChecked = false;
                MesOnOff.IsChecked = GlobalValues.MesOnOff;
                EnableGroupCheck.IsChecked = GlobalValues.MesGroupCheckOnOff;
                EnableVarInj.IsChecked = GlobalValues.EnableVarInj;
                DataUpload.IsChecked = GlobalValues.DataUpload;
                InjValStd.Value = GlobalValues.InjValStd;
                InjValMin.Value = GlobalValues.InjValMin;
                InjValMax.Value = GlobalValues.InjValMax;
                InjValTotalStd.Value = GlobalValues.InjValTotalStd;
                InjValTotalMin.Value = GlobalValues.InjValTotalMin;
                InjValTotalMax.Value = GlobalValues.InjValTotalMax;

                VectorMin.Value = GlobalValues.VectorMin;
                VectorMax.Value = GlobalValues.VectorMax;

                EnableBeforInjectCheck.IsChecked = GlobalValues.EnableBeforeInjectCheck;
                EnableAfterInjectCheck.IsChecked = GlobalValues.EnableAfterInjectCheck;
                BeforeWeightMin.Value = GlobalValues.BeforeWeightMin;
                BeforeWeightMax.Value = GlobalValues.BeforeWeightMax;
                AfterWeightMin.Value = GlobalValues.AfterWeightMin;
                AfterWeightMax.Value = GlobalValues.AfterWeightMax;
                this.OpCode.Text = "LiqiudInjectionA|LiqiudInjectionB|LiqiudInjectionC";//GlobalValues.GroupCode; 
                this.mescheckurl.Text = GlobalValues.MESUrl;
                working3.IsChecked = GlobalValues.Rework;
                if (GlobalValues.TestType == 1)
                {
                    testType1.IsChecked = true;
                }
                else
                {
                    testType2.IsChecked = true;
                }

                if (GlobalValues.OpType.ToUpper() == "YC")
                {
                    working1.IsChecked = true;
                }
                else if (GlobalValues.OpType.ToUpper() == "EC")
                {
                    working2.IsChecked = true;
                }
                else if (GlobalValues.OpType.ToUpper() == "SC")
                {
                    working23.IsChecked = true;
                }

                if (GlobalValues.ProductModel.ToUpper() == "ONLINE")
                {
                    uploadmodel1.IsChecked = true;
                }
                else if (GlobalValues.ProductModel.ToUpper() == "OFFLINE")
                {
                    uploadmodel2.IsChecked = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                if (GlobalValues.EnableVarInj != EnableVarInj.IsChecked)
                {
                    Logger.Info(string.Format("{0} 修改了【启用变量注液】参数 {1}", GlobalValues.EmpNo, EnableVarInj.IsChecked));
                }
                if (GlobalValues.DataUpload != DataUpload.IsChecked)
                {
                    Logger.Info(string.Format("{0} 修改了【启用数据上传】参数 {1}", GlobalValues.EmpNo, DataUpload.IsChecked));
                }
                if (GlobalValues.MesOnOff != MesOnOff.IsChecked)
                {
                    Logger.Info(string.Format("{0} 修改了【Mes在线/离线】参数 {1}", GlobalValues.EmpNo, MesOnOff.IsChecked));
                }
                if (GlobalValues.MesGroupCheckOnOff != EnableGroupCheck.IsChecked)
                {
                    Logger.Info(string.Format("{0} 修改了【组别校验开关】参数 {1}", GlobalValues.EmpNo, EnableGroupCheck.IsChecked));
                }
                //Logger.Info($"{(BatchNoCheck.IsChecked==true?"取消":"启用")}了【电解液校验】");

                
                GlobalValues.InjValStd = (float)InjValStd.Value;
                GlobalValues.InjValMin = (float)InjValMin.Value;
                GlobalValues.InjValMax = (float)InjValMax.Value;
                GlobalValues.InjValTotalStd = (float)InjValTotalStd.Value;
                GlobalValues.InjValTotalMin = (float)InjValTotalMin.Value;
                GlobalValues.InjValTotalMax = (float)InjValTotalMax.Value;

                GlobalValues.VectorMin = (float)VectorMin.Value;
                GlobalValues.VectorMax = (float)VectorMax.Value;

                GlobalValues.EnableBeforeInjectCheck = EnableBeforInjectCheck.IsChecked;
                GlobalValues.EnableAfterInjectCheck = EnableAfterInjectCheck.IsChecked;
                GlobalValues.BeforeWeightMin = (double)BeforeWeightMin.Value;
                GlobalValues.BeforeWeightMax = (double)BeforeWeightMax.Value;
                GlobalValues.AfterWeightMin = (double)AfterWeightMin.Value;
                GlobalValues.AfterWeightMax = (double)AfterWeightMax.Value;
                GlobalValues.Rework = bool.Parse(working3.IsChecked.ToString());
                GlobalValues.MESUrl = this.mescheckurl.Text.Trim();
                GlobalValues.OpCode = this.OpCode.Text.Trim();
                if (MesOnOff.IsChecked == true)
                {
                    GlobalValues.MesOnOff = true;
                }
                else
                {
                    GlobalValues.MesOnOff = false;
                }
                if (EnableGroupCheck.IsChecked == true)
                {
                    GlobalValues.MesGroupCheckOnOff = true;
                }
                else
                {
                    GlobalValues.MesGroupCheckOnOff = false;
                }
                if (DataUpload.IsChecked == true)
                {
                    GlobalValues.DataUpload = true;
                }
                else
                {
                    GlobalValues.DataUpload = false;
                }
                if (working1.IsChecked == true)
                {
                    if (GlobalValues.OpType != "YC")
                        Logger.Info(string.Format("{0} 修改了【工序】参数 {1}", GlobalValues.EmpNo, "YC"));
                    GlobalValues.OpType = "YC";
                }
                else if (working2.IsChecked == true)
                {
                    if (GlobalValues.OpType != "EC")
                        Logger.Info(string.Format("{0} 修改了【工序】参数 {1}", GlobalValues.EmpNo, "EC"));
                    GlobalValues.OpType = "EC";
                }
                else if (working23.IsChecked == true)
                {
                    if (GlobalValues.OpType != "SC")
                        Logger.Info(string.Format("{0} 修改了【工序】参数 {1}", GlobalValues.EmpNo, "SC"));
                    GlobalValues.OpType = "SC";
                }
                if (uploadmodel1.IsChecked == true)
                {
                    if (GlobalValues.ProductModel != "ONLINE")
                        Logger.Info(string.Format("{0} 修改了【上传模式】参数 {1}", GlobalValues.EmpNo, "ONLINE"));
                    GlobalValues.ProductModel = "ONLINE";
                }
                else if (uploadmodel2.IsChecked == true)
                {
                    if (GlobalValues.ProductModel != "OFFLINE")
                        Logger.Info(string.Format("{0} 修改了【上传模式】参数 {1}", GlobalValues.EmpNo, "OFFLINE"));
                    GlobalValues.ProductModel = "OFFLINE";
                }
                if (EnableVarInj.IsChecked == true)
                {
                    if (GlobalValues.OpType.ToUpper() != "EC" && GlobalValues.OpType.ToUpper() != "SC")
                    {
                        MessageBox.Show("启用变量注液前,请先设定工序参数为【二次/三次注液】", "错误");
                        return;
                    }
                    if (GlobalValues.InjValTotalStd <= 0 || GlobalValues.InjValTotalMin <= 0 || GlobalValues.InjValTotalMax <= 0)
                    {
                        MessageBox.Show("启用变量注液前,请先设定总注液量参数", "错误");
                        return;
                    }
                    GlobalValues.EnableVarInj = true;
                }
                else
                {
                    GlobalValues.EnableVarInj = false;
                }

                if (testType1.IsChecked == true)
                {
                    GlobalValues.TestType = 1;
                }
                else
                {
                    GlobalValues.TestType = 2;
                }

                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings.AllKeys.Contains("EnableVarInj"))
                {
                    config.AppSettings.Settings["EnableVarInj"].Value = GlobalValues.EnableVarInj.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("EnableVarInj", GlobalValues.EnableVarInj.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("TestType"))
                {
                    config.AppSettings.Settings["TestType"].Value = GlobalValues.TestType.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("TestType", GlobalValues.TestType.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("MesOnOff"))
                {
                    config.AppSettings.Settings["MesOnOff"].Value = GlobalValues.MesOnOff.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("MesOnOff", GlobalValues.MesOnOff.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("MesGroupCheckOnOff"))
                {
                    config.AppSettings.Settings["MesGroupCheckOnOff"].Value = GlobalValues.MesGroupCheckOnOff.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("MesGroupCheckOnOff", GlobalValues.MesGroupCheckOnOff.ToString());
                }
                if (config.AppSettings.Settings.AllKeys.Contains("DataUpload"))
                {
                    config.AppSettings.Settings["DataUpload"].Value = GlobalValues.DataUpload.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("DataUpload", GlobalValues.DataUpload.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("ProductModel"))
                {
                    config.AppSettings.Settings["ProductModel"].Value = GlobalValues.ProductModel.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("ProductModel", GlobalValues.ProductModel.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("InjValStd"))
                {
                    config.AppSettings.Settings["InjValStd"].Value = GlobalValues.InjValStd.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("InjValStd", GlobalValues.InjValStd.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("InjValMin"))
                {
                    config.AppSettings.Settings["InjValMin"].Value = GlobalValues.InjValMin.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("InjValMin", GlobalValues.InjValMin.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("InjValMax"))
                {
                    config.AppSettings.Settings["InjValMax"].Value = GlobalValues.InjValMax.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("InjValMax", GlobalValues.InjValMax.ToString());
                }

                //
                if (config.AppSettings.Settings.AllKeys.Contains("VectorMin"))
                {
                    config.AppSettings.Settings["VectorMin"].Value = GlobalValues.VectorMin.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("VectorMin", GlobalValues.VectorMin.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("VectorMax"))
                {
                    config.AppSettings.Settings["VectorMax"].Value = GlobalValues.VectorMax.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("VectorMax", GlobalValues.VectorMax.ToString());
                }


                if (config.AppSettings.Settings.AllKeys.Contains("InjValTotalStd"))
                {
                    config.AppSettings.Settings["InjValTotalStd"].Value = GlobalValues.InjValTotalStd.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("InjValTotalStd", GlobalValues.InjValTotalStd.ToString());
                }
                if (config.AppSettings.Settings.AllKeys.Contains("InjValTotalMin"))
                {
                    config.AppSettings.Settings["InjValTotalMin"].Value = GlobalValues.InjValTotalMin.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("InjValTotalMin", GlobalValues.InjValTotalMin.ToString());
                }
                if (config.AppSettings.Settings.AllKeys.Contains("InjValTotalMax"))
                {
                    config.AppSettings.Settings["InjValTotalMax"].Value = GlobalValues.InjValTotalMax.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("InjValTotalMax", GlobalValues.InjValTotalMax.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("OpType"))
                {
                    config.AppSettings.Settings["OpType"].Value = GlobalValues.OpType.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("OpType", GlobalValues.OpType.ToString());
                }



                if (config.AppSettings.Settings.AllKeys.Contains("EnableBeforeInjectCheck"))
                {
                    config.AppSettings.Settings["EnableBeforeInjectCheck"].Value = GlobalValues.EnableBeforeInjectCheck.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("EnableBeforeInjectCheck", GlobalValues.EnableBeforeInjectCheck.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("EnableAfterInjectCheck"))
                {
                    config.AppSettings.Settings["EnableAfterInjectCheck"].Value = GlobalValues.EnableAfterInjectCheck.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("EnableAfterInjectCheck", GlobalValues.EnableAfterInjectCheck.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("BeforeWeightMin"))
                {
                    config.AppSettings.Settings["BeforeWeightMin"].Value = GlobalValues.BeforeWeightMin.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("BeforeWeightMin", GlobalValues.BeforeWeightMin.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("BeforeWeightMax"))
                {
                    config.AppSettings.Settings["BeforeWeightMax"].Value = GlobalValues.BeforeWeightMax.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("BeforeWeightMax", GlobalValues.BeforeWeightMax.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("AfterWeightMin"))
                {
                    config.AppSettings.Settings["AfterWeightMin"].Value = GlobalValues.AfterWeightMin.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("AfterWeightMin", GlobalValues.AfterWeightMin.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("AfterWeightMax"))
                {
                    config.AppSettings.Settings["AfterWeightMax"].Value = GlobalValues.AfterWeightMax.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("AfterWeightMax", GlobalValues.AfterWeightMax.ToString());
                }
                if (config.AppSettings.Settings.AllKeys.Contains("Rework"))
                {
                    config.AppSettings.Settings["Rework"].Value = GlobalValues.Rework.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("Rework", GlobalValues.Rework.ToString());
                }
                //MESUrl
                if (config.AppSettings.Settings.AllKeys.Contains("MESUrl"))
                {
                    config.AppSettings.Settings["MESUrl"].Value = GlobalValues.MESUrl.ToString();
                }
                else
                {
                    config.AppSettings.Settings.Add("MESUrl", GlobalValues.MESUrl.ToString());
                }

                if (config.AppSettings.Settings.AllKeys.Contains("GroupCode"))
                {
                    if (!string.IsNullOrEmpty(OpCode.Text.Trim()))
                    {
                        config.AppSettings.Settings["GroupCode"].Value = GlobalValues.OpCode.ToString();
                    }

                }
                else
                {
                    if (!string.IsNullOrEmpty(OpCode.Text.Trim()))
                    {
                        config.AppSettings.Settings.Add("GroupCode", GlobalValues.OpCode.ToString());
                    }

                }
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
                Logger.Info(string.Format("注液参数 标准:{0},最小:{1},最大:{2},总量标准:{3},总量最小:{4},总量最大:{5}",
                    GlobalValues.InjValStd, GlobalValues.InjValMin, GlobalValues.InjValMax, GlobalValues.InjValTotalStd,
                    GlobalValues.InjValTotalMin, GlobalValues.InjValTotalMax));
                var groupCode = ConfigurationManager.AppSettings["GroupCode"];
                if (!string.IsNullOrEmpty(groupCode))
                {
                    var code = groupCode.Split('|');

                    if (GlobalValues.OpType.ToUpper() == "YC")
                    {
                        if (code.Length > 0)
                        {
                            GlobalValues.GroupCode = code[0];
                        }
                    }
                    else if (GlobalValues.OpType.ToUpper() == "EC")
                    {
                        if (code.Length > 1)
                        {
                            GlobalValues.GroupCode = code[1];
                        }
                    }
                    else if (GlobalValues.OpType.ToUpper() == "SC")
                    {
                        if (code.Length > 1)
                        {
                            GlobalValues.GroupCode = code[2];
                        }
                    }
                }

                Messenger.Default.Send<string>("", "ChangeTitle");
                MessageBox.Show("保存成功 ", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            try
            {
                var win = new ModifyPasswdView();
                win.ShowDialog();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            string scanResult = this.meschecktestcode.Text;
            var checkResult1 = MESCheck.GroupNameCheck(scanResult, GlobalValues.GroupName, GlobalValues.MoNumber, GlobalValues.GroupCode, GlobalValues.EmpNo);
            Logger.Info(string.Format("组别校验{0},{1},{2}.结果：{3}", scanResult, GlobalValues.EquNo, GlobalValues.EmpNo, checkResult1));
            if (checkResult1.Contains("TRUE"))
            {
                Logger.Info(string.Format("组别校验{0},{1},{2}.结果：{3}", scanResult, GlobalValues.EquNo, GlobalValues.EmpNo, checkResult1));
            }
            else
            {
                Logger.Info(string.Format("组别校验{0},{1},{2}.结果：{3}", scanResult, GlobalValues.EquNo, GlobalValues.EmpNo, checkResult1));
                return;
            }
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            string scanResult = this.meschecktestcode.Text;
            var ItemBatchNo = "|" + GlobalValues.ItemNo + ":" + GlobalValues.ItemBatchNo + ";" + GlobalValues.ItemNum;
            var checkResult2 = MESCheck.CellBOMItemsCheck(scanResult, GlobalValues.MoNumber, GlobalValues.EmpNo, GlobalValues.EquNo, ItemBatchNo);
            Logger.Info(string.Format("物料校验{0},{1},{2}.结果：{3}", scanResult, GlobalValues.EquNo, GlobalValues.EmpNo, checkResult2));
            if (checkResult2.Contains("TRUE"))
            {
                Logger.Info(string.Format("物料校验{0},{1},{2}.结果：{3}", scanResult, GlobalValues.EquNo, GlobalValues.EmpNo, checkResult2));
            }
            else
            {
                Logger.Info(string.Format("物料校验{0},{1},{2}.结果：{3}", scanResult, GlobalValues.EquNo, GlobalValues.EmpNo, checkResult2));
                return;
            }
        }
    }

}
